<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 7:44 PM
 */
declare(strict_types=1);


namespace application\module\admin\controller;


use application\module\admin\model\CalendarModel;
use wshore\core\Response;

class calendar extends admin
{
    public function index()
    {
        $this->doLayoutAssign();
        $this->display();
    }

    /**
     * 获取列表
     * @return void
     */
    public function getlist()
    {
        $calendar = new CalendarModel(self::$user->id);
        $list = $calendar->getlist();
        foreach ($list as &$item) {
            $item['start'] = date('Y-m-d H:i:s', (int)$item['start']);
            $item['end'] = date('Y-m-d H:i:s', (int)$item['end']);
            $item['backgroundColor'] = intval($item['done']) ? '#99cc33' : '#3a87ad';
        }
        Response::export([
            'status' => 1,
            'message' => 'OK',
            'data' => $list,
            'now' => date('Y-m-d'),
        ]);
    }

    public function add(string $title, int $start, int $end)
    {
        $calendar = new CalendarModel(self::$user->id);
        if ($calendar->add($title, $start, $end)) {
            Response::success('添加成功', ['id' => $calendar->lastInsertId()]);
        } else {
            Response::failure('添加失败');
        }
    }

    public function rm(int $id)
    {
        $calendar = new CalendarModel(self::$user->id);
        $res = $calendar->rm($id);
        if ($res) {
            Response::success('删除成功');
        } else {
            Response::failure('删除失败:' . $calendar->error());
        }

    }

}